package com.baili.app.ycbl.plugin.gensee

import android.util.Log
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.PluginRegistry

/**
 * 展示互动直播入口插件
 */
class GenseeLivePlugin : FlutterPlugin, ActivityAware {

    companion object {
        /**
         * 通道名，要和 flutter 端保持一致
         */
        private const val CHANEL_NAME = "gensee_live_plugin"

        @JvmStatic
            fun registerWith(registrar: PluginRegistry.Registrar) {
                Log.d("GenseeLivePlugin","registerWith")
                //使用 MethodChannel 通信
                val channel = MethodChannel(registrar.messenger(), CHANEL_NAME)
                val instance = GenseeMethodCallImpl(registrar.activity())
                channel.setMethodCallHandler(instance)
        }

    }

    private lateinit var flutterPluginBinding: FlutterPlugin.FlutterPluginBinding

    override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        log("onAttachedToEngine")
        this.flutterPluginBinding = binding
    }

    override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
        log("onDetachedFromEngine")
    }

    override fun onDetachedFromActivity() {
        log("onDetachedFromActivity")
    }

    override fun onReattachedToActivityForConfigChanges(binding: ActivityPluginBinding) {
        log("onReattachedToActivityForConfigChanges")
        onAttachedToActivity(binding)
    }

    override fun onAttachedToActivity(binding: ActivityPluginBinding) {
        log("onAttachedToActivity")
        //使用 MethodChannel 通信
        val channel = MethodChannel(flutterPluginBinding.binaryMessenger, CHANEL_NAME)
        val instance = GenseeMethodCallImpl(binding.activity)
        channel.setMethodCallHandler(instance)
    }

    override fun onDetachedFromActivityForConfigChanges() {

    }

    private fun log(msg: Any?) {
        Log.d("GenseeLivePlugin", "$msg")
    }

}
